#!/bin/bash
construct_path=$(cd `dirname $0`; pwd)
keepalive_path=`cd ../ && pwd`

localIp=$1
opposite=$2

#用于多网卡服务器处理
network="eth0"
function ips(){
	local nics=$(route -n | grep ^0.0.0.0 | awk '{print $8}')
	for nic in $nics
	do
	    local ip=$(ifconfig $nic | grep -E 'inet\s+' | awk '{print $2}')
		if [ $ip = "$1" ]; then
			network=$nic
		fi
	done
}
ips $localIp
#放开浮动ip的224.0.0.18的访问
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface $network --destination 224.0.0.18 --protocol vrrp -j ACCEPT


mkdir -p /etc/keepalived/
cp -rf $keepalive_path/config/health_check.sh /etc/keepalived/
cp -rf $keepalive_path/config/switch_backup.sh /etc/keepalived/
cp -rf $keepalive_path/config/switch_master.sh /etc/keepalived/
cp -rf $keepalive_path/config/keepalived_point2point.conf /etc/keepalived/

mv /etc/keepalived/keepalived_point2point.conf /etc/keepalived/keepalived.conf
chmod -R 644 /etc/keepalived/keepalived.conf
chmod -R 777 /etc/keepalived/*.sh

sed -i "s/network/$network/g" /etc/keepalived/keepalived.conf
sed -i "s/localIp/$localIp/g" /etc/keepalived/keepalived.conf
sed -i "s/opposite/$opposite/g" /etc/keepalived/keepalived.conf
